/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.tvremote.shortcuts;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.apps.tvremote.R;
import com.google.android.apps.tvremote.StartupActivity;
import com.google.android.apps.tvremote.shortcuts.ShortcutActions.ShortcutAction;

public class LauncherShortcuts extends Activity
{
    public static final String SHORTCUT_ACTION = "com.google.android.apps.tvremote.LauncherShortcuts";
    LayoutInflater mInflater;

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);

        mInflater = getLayoutInflater();

        setContentView(R.layout.launcher_shortcuts);
        ListView lv = (ListView) findViewById(R.id.shortcutList);

        lv.setAdapter(new ShortcutsAdapter(this, ShortcutActions.actions));

    }

    private void createShortcut(ShortcutActions.ShortcutAction action)
    {
        Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
        shortcutIntent.setClass(this, StartupActivity.class);
        shortcutIntent.putExtra(Intent.EXTRA_KEY_EVENT, action.action.ordinal());
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

        // Then, set up the container intent (the response to the caller)

        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, action.name);
        Parcelable iconResource = ((BitmapDrawable) getResources().getDrawable(action.iconResourceId)).getBitmap();
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconResource);

        // Now, return the result to the launcher

        setResult(RESULT_OK, intent);
        finish();
    }

    private class ShortcutsAdapter extends ArrayAdapter<ShortcutActions.ShortcutAction>
    {

        public ShortcutsAdapter(Context context, List<ShortcutActions.ShortcutAction> objects)
        {
            super(context, R.layout.launcher_shortcut_list, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            final ShortcutAction sa = getItem(position);

            if (convertView == null)
            {
                convertView = mInflater.inflate(R.layout.launcher_shortcut_list, null);
            }

            ImageView icon = (ImageView) convertView.findViewById(R.id.img);
            TextView title = (TextView) convertView.findViewById(R.id.title);

            icon.setImageResource(sa.iconResourceId);
            title.setText(sa.name);

            convertView.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    createShortcut(sa);
                }
            });

            return convertView;
        }
    }
}
